package com.goscom.app.deeset;

import com.goscom.app.database.DBAdapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class StoreInduction extends Activity {

	private String strMerId = "";
	private String strActivity = "";
	private DBAdapter mDB;
	private CheckBox ckbAll, ckb1, ckb2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDB = new DBAdapter(StoreInduction.this);
		mDB.open();

		getMerId();
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
			strActivity = getIntent().getStringExtra("activity");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId != null && strActivity != null) {
			ContentValues content = new ContentValues();
			if (strActivity.equals("storeinduction") == true) {
				setContentView(R.layout.storeinduction);
				ckbAll = (CheckBox) findViewById(R.id.ckbStoInducAll);
				content = mDB.queryDataByMerAddOther("STOREINDUCTION",
						strMerId, strActivity);
			}
			if (strActivity.equals("deesetprocedure") == true) {
				setContentView(R.layout.deesetprocedure);
				ckbAll = (CheckBox) findViewById(R.id.ckbDeeProAll);
				content = mDB.queryDataByMerAddOther("STOREINDUCTION",
						strMerId, strActivity);
			}
			if (strActivity.equals("otherdeeset") == true) {
				setContentView(R.layout.otherdeeset);
				ckbAll = (CheckBox) findViewById(R.id.ckbOthDeeAll);
				ckb1 = (CheckBox) findViewById(R.id.ckbOthDee1);
				content = mDB.queryDataByMerAddOther("STOREINDUCTION",
						strMerId, strActivity);
				if (content != null) {
					if (content.getAsString("check1").equals("1")) {
						ckb1.setChecked(true);
					}
				}
			}
			if (strActivity.equals("healthandsafety") == true) {
				setContentView(R.layout.healthandsafety);
				ckbAll = (CheckBox) findViewById(R.id.ckbHeaSafAll);
				ckb1 = (CheckBox) findViewById(R.id.ckbHeaSaf1);
				ckb2 = (CheckBox) findViewById(R.id.ckbHeaSaf2);
				content = mDB.queryDataByMerAddOther("STOREINDUCTION",
						strMerId, strActivity);
				if (content != null) {
					if (content.getAsString("check1").equals("1")) {
						ckb1.setChecked(true);
					}
					if (content.getAsString("check2").equals("1")) {
						ckb2.setChecked(true);
					}
				}
			}
			if (strActivity.equals("manual") == true) {
				setContentView(R.layout.manual);
				ckbAll = (CheckBox) findViewById(R.id.ckbManualAll);
				content = mDB.queryDataByMerAddOther("STOREINDUCTION",
						strMerId, strActivity);
			}
			if (content != null) {
				if (content.getAsString("checkall").equals("1")) {
					ckbAll.setChecked(true);
				}
			}
		}
	}

	public void Logout(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(StoreInduction.this, Login.class));
	}

	public void goPrevious(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		Intent intent = new Intent(StoreInduction.this, StoreInduction.class);
		intent.putExtra("merid", strMerId);
		if (strActivity.equals("storeinduction") == true) {
			startActivity(new Intent(StoreInduction.this, Additional.class)
					.putExtra("merid", strMerId));
		}
		if (strActivity.equals("deesetprocedure") == true) {
			intent.putExtra("activity", "storeinduction");
			startActivity(intent);
		}
		if (strActivity.equals("otherdeeset") == true) {
			intent.putExtra("activity", "deesetprocedure");
			startActivity(intent);
		}
		if (strActivity.equals("healthandsafety") == true) {
			intent.putExtra("activity", "otherdeeset");
			startActivity(intent);
		}
		if (strActivity.equals("manual") == true) {
			intent.putExtra("activity", "healthandsafety");
			startActivity(intent);
		}
	}

	public void goNext(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		Intent intent = new Intent(StoreInduction.this, StoreInduction.class);
		intent.putExtra("merid", strMerId);
		if (strActivity.equals("storeinduction") == true) {
			intent.putExtra("activity", "deesetprocedure");
			startActivity(intent);
		}
		if (strActivity.equals("deesetprocedure") == true) {
			intent.putExtra("activity", "otherdeeset");
			startActivity(intent);
		}
		if (strActivity.equals("otherdeeset") == true) {
			intent.putExtra("activity", "healthandsafety");
			startActivity(intent);
		}
		if (strActivity.equals("healthandsafety") == true) {
			intent.putExtra("activity", "manual");
			startActivity(intent);
		}
		if (strActivity.equals("manual") == true) {
			startActivity(new Intent(StoreInduction.this, Administration.class)
					.putExtra("merid", strMerId));
		}
	}

	public void Save(View v) {
		saveDB();
		Toast.makeText(StoreInduction.this, "Save Successful!",
				Toast.LENGTH_LONG).show();
	}

	public void saveDB() {
		if (mDB.queryMerchandiserAddOther("STOREINDUCTION", strMerId, strActivity) == 0) {
			insertDB();
		} else {
			mDB.deleteDataAddOther("STOREINDUCTION", strMerId, strActivity);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		insertAttributeDB(content);
		content.put("merid", strMerId);
		content.put("checkall", ckbAll.isChecked());
		content.put("activity", strActivity);
		mDB.insertData("STOREINDUCTION", content);
	}

	public void insertAttributeDB(ContentValues content) {
		if (strActivity.equals("healthandsafety") == true) {
			content.put("check1", ckb1.isChecked());
			content.put("check2", ckb2.isChecked());
		} else {
			if (strActivity.equals("otherdeeset") == true) {
				content.put("check1", ckb1.isChecked());
				content.put("check2", "2");
			} else {
				content.put("check1", "2");
				content.put("check2", "2");
			}
		}
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}
}
